//WorkRegistry.cpp
//
#include "StdAfx.h"
#include "Work.h"

WorkRegistry::WorkRegistry(){
}

WorkRegistry::~WorkRegistry(){
	RegistryContain::iterator itr;	

	for( itr=registry_.begin() ; itr != registry_.end() ; itr++ ){		
	}

	registry_.clear();
}

bool WorkRegistry::add(WorkListener * listener){
	IS_NULL_RETURN(listener, false);
	
	RegistryContain::iterator itr = registry_.find( listener->getType() );
	if( itr != registry_.end() )
		return false;

    std::pair<RegistryContain::iterator, bool > pr;
    pr = registry_.insert(std::pair<size_t, WorkListener*>(listener->getType(), listener));

    return pr.second;
}

void WorkRegistry::remove(size_t type){
    RegistryContain::iterator itr;
    itr=registry_.find(type);
    if( itr != registry_.end() ) 
		registry_.erase(itr);
}

void WorkRegistry::cry(Work * work, void * arg){
    RegistryContain::iterator itr;
    itr=registry_.find(work->getType());
    if( itr == registry_.end() ) 
		return;
    
    WorkListener * listener = itr->second;
    listener->workPerformed(work, arg); 
}

void WorkRegistry::specificCry(Work * work, void * arg){
	RegistryContain::iterator itr;
    itr=registry_.find(work->getType());
    if( itr == registry_.end() )
		return;
    
    WorkListener * listener = itr->second;
    listener->specificPerformed(work, arg);
}

void WorkRegistry::threadCry( Work* work, void* arg ){
	RegistryContain::iterator itr;
    itr=registry_.find( work->getType() );
    if( itr == registry_.end() )
		return;
    
    WorkListener * listener = itr->second;
    listener->threadPerformed(work, arg);
}

void WorkRegistry::spareCry( Work* work, void* arg ){
	RegistryContain::iterator itr;
    itr=registry_.find(work->getType());
    if( itr == registry_.end() )
		return;
    
    WorkListener * listener = itr->second;
    listener->sparePerformed(work, arg);
}

void WorkRegistry::orderCry( Work* work, void* arg ){
	RegistryContain::iterator itr;
    itr=registry_.find( 0 );

    if( itr == registry_.end() )
		return;
    
    WorkListener * listener = itr->second;
    listener->orderPerformed( work, arg );
}

void WorkRegistry::cry_all( Work * work, void * arg ){
	WorkListener* listener = 0;
	RegistryContain::iterator itr;

	for( itr = registry_.begin() ; itr != registry_.end() ; itr++ ){
		listener = (WorkListener*)itr->second;

		IS_NULL_ACTION( listener, continue );
		listener->workPerformed( work, arg );
	}
}

void WorkRegistry::specificCry_all( Work * work, void * arg ){	
	WorkListener* listener = 0;
	RegistryContain::iterator itr;

	for( itr = registry_.begin() ; itr != registry_.end() ; itr++ ){
		listener = (WorkListener*)itr->second;

		IS_NULL_ACTION( listener, continue );
		listener->specificPerformed( work, arg );
	}   
}

void WorkRegistry::threadCry_all( Work* work, void* arg ){	
	WorkListener* listener = 0;
	RegistryContain::iterator itr;

	for( itr = registry_.begin() ; itr != registry_.end() ; itr++ ){
		listener = (WorkListener*)itr->second;

		IS_NULL_ACTION( listener, continue );
		listener->threadPerformed( work, arg );
	}   
}

void WorkRegistry::spareCry_all( Work* work, void* arg ){	
	WorkListener* listener = 0;
	RegistryContain::iterator itr;

	for( itr = registry_.begin() ; itr != registry_.end() ; itr++ ){
		listener = (WorkListener*)itr->second;

		IS_NULL_ACTION( listener, continue );
		listener->sparePerformed( work, arg );
	}   
}

size_t WorkRegistry::size(){
	return registry_.size();
}
